<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/web/ui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/web/ui/css/public.css" media="all">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
    <script src="/web/js/vue2.js"></script>
    <script src="/web/js/axios.min.js"></script>
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>

</head>
<body>
<div id="app" class="layui-col-md-offset4">
    <div class="layui-form layuimini-form">

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
            <template>
                <el-checkbox-group v-model="checkedIdList" v-for="r of allRoleList">
                    <el-checkbox :label="r.id">{{r.name}}</el-checkbox>
                </el-checkbox-group>
            </template>
        </div>
    </div>


</div>
<script src="/web/ui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>


<script>

    const user = JSON.parse(localStorage.getItem("t"));
    localStorage.removeItem("t");

    const vm = new Vue({
        el: "#app",
        data() {
            return {
                allRoleList: [],
                checkedIdList: [],
            }
        },
        mounted() {
            axios.get("/role/pre/assign?userId=" + user.id)
                .then(res => {
                    this.allRoleList = res.data.data.allRoleList;
                    let ownRoleList = res.data.data.ownRoleList;
                    for (let r of ownRoleList) {
                        // console.log(m.id);
                        this.checkedIdList.push(r.id);
                    }
                    // console.log(this.checkedIdList);
                })
        }
    })

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data.field = vm.$data.checkedIdList;
            var index = layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息' + vm.$data.checkedIdList
            }, async function () {

                await axios.post(`/role/to/assign?ids=${data.field}&userId=${user.id}`)
                    .then(res=>{
                    })

                // 关闭弹出层
                layer.close(index);

                // 刷新父页面的父页面【顶层页面】
                window.parent.parent.location.reload();
                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);

            });

            return false;
        });

    });
</script>
</body>
</html>